﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.TCP;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;

namespace 模拟客户端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public object SaveDataProcessCallbackProc(byte[] tempbuffer, ref byte[] buffer, ref int dataOffset, int length)
        {
            //判断读取的字节数+缓冲区已有字节数是否超过缓冲区总大小
            if (length + dataOffset > buffer.Length)
            {
                Array.Resize<Byte>(ref buffer, buffer.Length + 1024);
            }
            //将新读取的数据拷贝到缓冲区
            Array.Copy(tempbuffer, 0, buffer, dataOffset, length);
            //修改"数据实际长度"
            dataOffset += length;
            string strRx = Encoding.Default.GetString(buffer, 0, dataOffset);
            if (strRx.IndexOf("#") == -1)
                return null;

            //获得了完整的数据包            
            Array.Copy(buffer, strRx.Length, buffer, 0, dataOffset - (strRx.Length));
            dataOffset -= (strRx.Length);//缓冲区实际数据长度减去一个完整封包长度
            return strRx;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
            try
            {
                Thread ReaderThreadIns = new Thread(new ThreadStart(a));
                ReaderThreadIns.Start(); 
            }
            catch
            {

            }
                     
        }
        private void a()
        {
            int x=1;
            while(true)
            {
                try
                {
                    Thread thr = new Thread(ReaderProc);
                    thr.Start((object)x++);
                    if (x == 100)
                        break;
                    Thread.Sleep(10);
                }
                catch (System.Exception ex)
                {
                	
                }
                
            }
        }
        private void ReaderProc(object a)
        {
            while(true)
            {
                TCPSyncClient cliet = new TCPSyncClient();
                cliet.TCPServerName = "hrbylsl.oicp.net";//"127.0.0.1";
                cliet.TCPServerPort = 8001;

                cliet.SaveDataProcessCallback = new SaveDataProcessCallbackDelegate(SaveDataProcessCallbackProc);
                cliet.Conn();

                cliet.SendPacket(Encoding.Default.GetBytes(a.ToString()+"#"));
                cliet.ReadTimeOut = 2000;
                string str;
                try
                {
                    str = cliet.ReceivePacket().ToString();
                    Debug.WriteLine(str);
//                     this.Invoke((EventHandler)(delegate
//                     {
//                         textBox1.AppendText(str + "\r\n");
//                     }));
                }
                catch (System.Exception ex)
                {
//                     this.Invoke((EventHandler)(delegate
//                     {
//                         textBox1.AppendText(a.ToString() + "通信失败" + "\r\n");
//                     }));
                }
                

                cliet.Close();
                Thread.Sleep(10);
            }
        }
    }
}
